
/**
* Extend the array Object
* Array::find()
*
*/
var ExtArray = {
  
  /**
  * php array_search like
  * 
  * @param  String  needle
  * @param  String  haystack
  * 
  * @return integer the index of array of this needle in
  * 
  */
  arraySearch: function(needle, haystack){
    
    var nLoop   = haystack.length;
    var retInt  = false;
    
    for(i = 0; i < nLoop; i++){
      
      if(haystack[i] != needle){
      
        //alert(haystack[i].length + " <> " + needle.length);
      }else{
        
        retInt = i;
        break;
      }
    }
    
    return retInt;
  },
  
  trim : function (str){  
    
    while(str.charAt(0) == (" ") ){  
      
      str = str.substring(1);
    }
    
    while(str.charAt(str.length-1) == " " ){  
      
      str = str.substring(0,str.length-1);
    }
    
    return str;
  }
}

var Ajax = {
  
  _CONN_ERROR           : "****",
  _RET_NULL             : " ",
  _IS_EXISTS            : "EXISTS",
  _NOT_EXISTS           : "0",
  _DEFAULT_SEPARATOR_02 : "~^~",
  _LEVEL_MN             : 0,
  _STATUS               : 1,
  _ENTRY_RIGHTS         : 2,
  _MODIFY_RIGHTS        : 3,
  _DELETE_RIGHTS        : 4,
  _VIEW_RIGHTS          : 5,
  _FINALIZE_RIGHTS      : 6,
  _APPROVED_RIGHTS      : 7,
  _JOURNAL_RIGHTS       : 8,
  _PROGRAMS             : 9,
  _PICT                 : 10,
  nTimes                : 0,
  retBuf                : new Array(),
  
  groupAlloc : function(){
    
    var _LEVELMN_ENT  = "levelMnEnt";
    var _USER_POS     = "usrPosEnt";
    var _USER_DEPT    = "usrDeptEnt"; 
    
    /* this.showBusyBox(true); */
    
    /**
    * get the pos and dept
    * fetch the template
    *
    */
    var srcPos  = document.getElementsByName(_USER_POS);
    var srcDept = document.getElementsByName(_USER_DEPT);
    
    if(srcPos.length > 0){
      
      srcPos = srcPos[0];
    }
    
    if(srcDept.length > 0){
      
      srcDept = srcDept[0];
    }
    /**
    * fetch how many times do
    * do we have to loop
    *
    */
    this.nTimes = this.initPrivsList(srcPos.value,srcDept.value);
   
    for(iRow = 0; iRow < this.nTimes; iRow++){
      
      /**
      * fetch the levelMn
      * and the privs
      * copy to form
      *
      */
      this.retBuf = this.fetchPrivsList(iRow);

      this.retBuf = this.retBuf.split(this._DEFAULT_SEPARATOR_02);
      
      var levelMn = this.levelMnList(_LEVELMN_ENT);
      
      var idxLevelMn = ExtArray.arraySearch(ExtArray.trim(this.retBuf[this._LEVEL_MN]), 
                                            levelMn);
      
      var statusEnt   = document.getElementsByName("statusEnt["   + idxLevelMn +"]");
      var entryEnt    = document.getElementsByName("entryEnt["    + idxLevelMn +"]");
      var modifyEnt   = document.getElementsByName("modifyEnt["   + idxLevelMn +"]");
      var deleteEnt   = document.getElementsByName("deleteEnt["   + idxLevelMn +"]");
      var viewEnt     = document.getElementsByName("viewEnt["     + idxLevelMn +"]");
      var finalizeEnt = document.getElementsByName("finalizeEnt[" + idxLevelMn +"]");
      var approvedEnt = document.getElementsByName("approvedEnt[" + idxLevelMn +"]");
      var journalEnt  = document.getElementsByName("journalEnt["  + idxLevelMn +"]");
      var programsEnt = document.getElementsByName("programsEnt[" + idxLevelMn +"]");
      var pictEnt     = document.getElementsByName("pictEnt["     + idxLevelMn +"]");
      
      if(parseInt(this.retBuf[this._STATUS]) == 1){
        
        statusEnt[0].checked   = true;
      }else{
        
        statusEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._ENTRY_RIGHTS]) == 1){
        
        entryEnt[0].checked   = true;
      }else{
        
        entryEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._MODIFY_RIGHTS]) == 1){
        
        modifyEnt[0].checked   = true;
      }else{
        
        modifyEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._DELETE_RIGHTS]) == 1){
        
        deleteEnt[0].checked   = true;
      }else{
        
        deleteEnt[0].checked   = false;
      }
                               
      if(parseInt(this.retBuf[this._VIEW_RIGHTS]) == 1){
        
        viewEnt[0].checked   = true;
      }else{
        
        viewEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._FINALIZE_RIGHTS]) == 1){
        
        finalizeEnt[0].checked   = true;
      }else{
        
        finalizeEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._APPROVED_RIGHTS]) == 1){
        
        approvedEnt[0].checked   = true;
      }else{
        
        approvedEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._JOURNAL_RIGHTS]) == 1){
        
        journalEnt[0].checked   = true;
      }else{
        
        journalEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._PROGRAMS]) == 1){
        
        programsEnt[0].checked   = true;
      }else{
        
        programsEnt[0].checked   = false;
      }
      
      if(parseInt(this.retBuf[this._PICT]) == 1){
        
        pictEnt[0].checked   = true;
      }else{
        
        pictEnt[0].checked   = false;
      }
    }
    
    this.cleanUp();
    
    /* this.showBusyBox(false); */
  },
  
  /**
  * Init the privileges list
  *
  * @param  String  srcPos
  * @param  String  srcDept
  * 
  */
  initPrivsList : function(srcPos, srcDept){
    
    var xmlHttp =	new XMLHttpRequest();
	
  	var url 		= "/ecs/tocmenu_Y1/controllers/initPrivsList.php" + "?sid="+Math.random();
  	var params 	= "srcPos="   + srcPos +
  	              "&srcDept=" + srcDept;
  	
  	xmlHttp.open("POST", url + params, false);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {
  	  
  		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
  		
  	}
  	
  	xmlHttp.send(params);
  	
  	return xmlHttp.responseText;
  },
  
  /**
  * fetch the privilege list
  * also it clear out the previous entry 
  * automatically
  * 
  * @param  integer idx
  *
  */
  fetchPrivsList:function(idx){
    
    var xmlHttp =	new XMLHttpRequest();
	
  	var url 		= "/ecs/tocmenu_Y1/controllers/fetchPrivsList.php" + "?sid="+Math.random();
  	var params 	= "idx=" + idx;
  	
  	xmlHttp.open("POST", url, false);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {
  	  
  		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
  		
  	}
  	xmlHttp.send(params);
  	
  	return xmlHttp.responseText;
  },
  
  /**
  * cleanup the session var which being used
  * to stored groupList 
  * 
  * @param  void
  *
  */
  cleanUp:function(){
    
    var xmlHttp =	new XMLHttpRequest();
	
  	var url 		= "/ecs/tocmenu_Y1/controllers/cleanUp.php" + "?sid="+Math.random();
  	var params 	= " ";
  	
  	xmlHttp.open("POST", url, false);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {
  	  
  		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
  		
  		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  		
  			/* Do nothing */
  		}
  	}
  	xmlHttp.send(params);
  },
  
  /**
  * fetch as many as levelMnEnt
  * we could fetch
  * 
  * @param  String  levelMnEnt  the object name
  * 
  * @return Array
  *
  */
  levelMnList: function(levelMnEnt){
    
    var retStr = new Array();
    var isDone = false;
    var i      = 0;
    
    while(isDone == false){
      
      var strComplete = levelMnEnt + "[" + i + "]";
      
      var tmpBuf = document.getElementsByName(strComplete);
      
      if(tmpBuf.length > 0){
        
        retStr[i] = String(tmpBuf[0].value);
      }else{
        
        isDone = true;
      }
      
      i++;
    }
    
    return retStr;
  },
  
  isIdExists : function (thisObj){
  
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_Y1/controllers/isIdExists.php" +"?sid="+Math.random();
    var params  = "thisId=" + thisObj.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    var retVal = ExtArray.trim(xmlHttp.responseText);
    
    switch(retVal){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      case this._IS_EXISTS:
      
        alert("Id already Exists");
        thisObj.value = "";
        break;
        
      default:
        
        /* Do nothing */
    }
  },
  
  sayHello : function(){
  
    alert("Ajax linked");
  }
}